package com.ktjy.config;

import com.ktjy.entity.SysRole;
import com.ktjy.entity.SysUser;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

//定义配置类
@Configuration
@PropertySource(value = "classpath:user.properties",encoding = "utf-8")
public class JavaConfig {
    //使用@Value注解，将属性值注入到bean对象中
    @Value("${user.realName}")
    private String realName;
    @Value("${user.account}")
    private String account;
    @Value("${user.password}")
    private String password;
    @Value("${user.sex}")
    private int sex;

    //定义bean对象
    @Bean
    //也可以通过的注解的name属性修改bean名称
    //@Bean(name="user1")
    public SysUser user(@Qualifier("commonRole") SysRole commonRole){
        SysUser user=new SysUser();
        user.setRealName(realName);
        user.setAccount(account);
        user.setPassword(password);
        user.setSex(sex);
        user.setRole(commonRole);
        //返回对象会被注册到Spring容器中
        return user;
    }

    //注册管理员角色对象
    @Bean
    @Primary //设置当前对象为默认对象
    public SysRole admainRole(){
        SysRole role=new SysRole();
        role.setId(1L);
        role.setRoleName("管理员|");
        role.setCode("MANAGER");
        return role;
    }
    //注册普通用户角色对象
    @Bean
    public SysRole commonRole(){
        SysRole role=new SysRole();
        role.setId(2L);
        role.setRoleName("普通用户");
        role.setCode("COMMON");
        return role;
    }
}
